//
//  ViewController.m
//  TableViewOpenDemo
//
//  Created by offcn_zcz32036 on 2017/4/11.
//  Copyright © 2017年 offcn. All rights reserved.
//

#import "ViewController.h"
#import "MyGroupHeaderView.h"
#define kHeaderViewTag 100
@interface ViewController ()
<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView*myTableView;
@property(nonatomic,strong)NSMutableArray*mutData;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStylePlain];
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    [self.view addSubview:self.myTableView];
    self.mutData=[[NSMutableArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"GroupList" ofType:@"plist"]];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MyGroupHeaderView *groupHeaderView=(MyGroupHeaderView*)[tableView viewWithTag:kHeaderViewTag +section];
    if (!groupHeaderView) {
        groupHeaderView=[[MyGroupHeaderView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)];
        groupHeaderView.tag=kHeaderViewTag+section;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(groupHeaderTap:)];
        [groupHeaderView addGestureRecognizer:tap];
    }
    groupHeaderView.groupName=[[self.mutData objectAtIndex:section] objectForKey:@"groupName"];
    return groupHeaderView;
}
-(void)groupHeaderTap:(UITapGestureRecognizer*)tap
{
    MyGroupHeaderView *groupHeaderView=(MyGroupHeaderView*)tap.view;
    groupHeaderView.open=!groupHeaderView.open;
    NSInteger section=groupHeaderView.tag-kHeaderViewTag;
    [[self.mutData objectAtIndex:section] setObject:[NSNumber numberWithBool:groupHeaderView.open] forKey:@"open"];
    [self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.mutData.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic=[self.mutData objectAtIndex:section];
    BOOL open=[[dic objectForKey:@"open"]boolValue];
    NSInteger numberOfRows;
    if (open) {
        NSArray *memberList=[dic objectForKey:@"memberList"];
        numberOfRows=memberList.count;
    }
    else
    {
        numberOfRows=0;
    }
    return numberOfRows;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId=@"cellID";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    NSDictionary *groupDict=[self.mutData objectAtIndex:indexPath.section];
    NSArray *memberList=[groupDict objectForKey:@"memberList"];
    NSDictionary *memberDict=[memberList objectAtIndex:indexPath.row];
    cell.textLabel.text=[memberDict objectForKey:@"name"];
    cell.detailTextLabel.text=[memberDict objectForKey:@"desc"];
    cell.imageView.image=[UIImage imageNamed:[memberDict objectForKey:@"icon"]];
    return cell;
}

























@end
